package postjnlv2_param.controller;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JOptionPane;

import postjnlv2_param.dao.ArborCustD3Dao;
import postjnlv2_param.modele.DateConvertor;
import postjnlv2_param.modele.FormatDate;
import postjnlv2_param.view.PostJNL_Panel_View;
import postjnlv2_param.view.Rapport_Detail_View;
import postjnlv2_param.view.Rapport_Panel_View;



public class PostJNLController implements ActionListener{
	
	private PostJNL_Panel_View postJNL_Panel_View;	
	private Rapport_Detail_View rapport_Detail_View;	
	private Rapport_Panel_View rapport_Panel_View;	
	private ArborCustD3Dao arborCustD3Dao;
	private DateConvertor dateConvertor;
	private FormatDate formatDate;
	private String max_jnl_end_dt="";
	private String jnlRefNo1="",jnlRefNo2="";
	private Date dateNow;
	private String date1="",date2="";
	private String t_id_run="";
	private ArrayList<String> list_Max_Ref_no=null;
	
	public PostJNLController(PostJNL_Panel_View postJNL_Panel_View,Rapport_Panel_View rapport_Panel_View){
		this.postJNL_Panel_View= postJNL_Panel_View;
		this.rapport_Panel_View= rapport_Panel_View;
		arborCustD3Dao=new ArborCustD3Dao();
		rapport_Detail_View=new Rapport_Detail_View();
		dateConvertor=new DateConvertor();
		formatDate=new FormatDate();
		
	}	
	

	@Override
	public void actionPerformed(ActionEvent e) {
		postJNL_Panel_View.txtJnlEnDtResult.setText(null);
		postJNL_Panel_View.txtJnlRefNo1.setText(null);
		postJNL_Panel_View.txtJnlRefNo2.setText(null);
		postJNL_Panel_View.txtIdRunResult.setText(null);
		rapport_Panel_View.center_north_west.removeAll();
		rapport_Panel_View.center_south_west.removeAll();
		rapport_Panel_View.center_north_west.repaint();
		rapport_Panel_View.center_south_west.repaint();
		rapport_Detail_View.barreProgression.setValue(0);
		
				
		Thread t = new Thread() {
            public void run() {  
            postJNL_Panel_View.btValider.setEnabled(false);
            rapport_Detail_View= new Rapport_Detail_View();
            rapport_Panel_View.center_north_west.add(rapport_Detail_View.center_north,BorderLayout.WEST);
            rapport_Panel_View.center_south_west.add(rapport_Detail_View.center_south,BorderLayout.WEST);	       
            debutRapport();
	          infoJNL_END_DT();
	          infoJNL_REF_NO();
	          infoID_RUN();
	          infoUpdate1();
	          infoUpdate2();
	          infoModifProc();
	          recapRapport();
	          commitOrNot();
	          arborCustD3Dao.closeConnection();
            postJNL_Panel_View.btValider.setEnabled(true);
            }
          };
          t.start();	
	}		
	
	public void debutRapport(){		
		try {			
			rapport_Detail_View.lbTitle.setVisible(true);
			rapport_Detail_View.txt.setVisible(true);
            Thread.sleep(1000);
            progressBar(12);
          } catch (InterruptedException Te) {
        	  System.out.println(Te);
          }
	}
	
	public void infoJNL_END_DT(){		
		try {
			rapport_Detail_View.lbJnlEndDt.setVisible(true);
			rapport_Detail_View.lbEquiv1.setVisible(true);
			rapport_Detail_View.lbJnlEndDtRes.setVisible(true);
            Thread.sleep(1000);
            progressBar(24);
            max_jnl_end_dt=arborCustD3Dao.getMAX_JNL_END_DT_Dao();
            rapport_Detail_View.lbJnlEndDtRes.setText("[ ok ]");
            rapport_Detail_View.lbJnlEndDtRes.setForeground(Color.green);
            postJNL_Panel_View.txtJnlEnDtResult.setText(max_jnl_end_dt);
            String dateTmp=max_jnl_end_dt.substring(0,10);
            dateTmp=dateTmp.replaceAll("-", "");
            System.out.println("DATE TEMPORAIRE : "+dateTmp);
            date1=dateTmp;
            String month=dateConvertor.getMonthBefore(max_jnl_end_dt);
            System.out.println("MOIS PRECEDENT : "+month);
            date1=date1.replace(dateTmp.substring(4, 6), month);
            System.out.println("COMPLETE PRECEDENT COMPLETE: "+date1);           
            dateNow=new Date();
            date2=formatDate.dateToString(dateNow);
            System.out.println("DATE DU JOUR: "+date2);
            date2=date2.replace(date2.substring(6, 8), "01");
            System.out.println("DATE DU JOUR APRES MODIF: "+date2);
            
          } catch (InterruptedException Te) {
        	  System.out.println(Te);
          }			
	}
	
	public void infoJNL_REF_NO(){		
		try {
			Thread.sleep(1000);
			progressBar(36);
			rapport_Detail_View.lbJnlRefNo.setVisible(true);
			rapport_Detail_View.lbEquiv2.setVisible(true);
			rapport_Detail_View.lbJnlRefNoRes.setVisible(true);
            Thread.sleep(1000);
            list_Max_Ref_no=arborCustD3Dao.getMAX_JNL_REF_NO_Dao();
            rapport_Detail_View.lbJnlRefNoRes.setText("[ ok ]");
            rapport_Detail_View.lbJnlRefNoRes.setForeground(Color.green);	            
            postJNL_Panel_View.txtJnlRefNo1.setText(list_Max_Ref_no.get(0));
            postJNL_Panel_View.txtJnlRefNo2.setText(list_Max_Ref_no.get(1));
          } catch (InterruptedException Te) {
        	  System.out.println(Te);
          }			
	}
	
	public void infoID_RUN(){		
		try {
			Thread.sleep(1000);
			progressBar(48);
			jnlRefNo1=list_Max_Ref_no.get(0);
			jnlRefNo2=list_Max_Ref_no.get(1);
			rapport_Detail_View.lbIdRun.setVisible(true);
			rapport_Detail_View.lbEquiv3.setVisible(true);
			rapport_Detail_View.lbIdRunRes.setVisible(true);
            Thread.sleep(1000);
            t_id_run=arborCustD3Dao.getT_ID_RUN_Dao(jnlRefNo1, jnlRefNo2);
            rapport_Detail_View.lbIdRunRes.setText("[ ok ]");
            rapport_Detail_View.lbIdRunRes.setForeground(Color.green);
            postJNL_Panel_View.txtIdRunResult.setText(t_id_run);
            } catch (InterruptedException Te) {
        	  System.out.println(Te);
          }			
	}
	
	public void infoUpdate1(){		
		try {
			Thread.sleep(1000);
			progressBar(60);
			rapport_Detail_View.lbUpdate1.setVisible(true);
			rapport_Detail_View.lbEquiv4.setVisible(true);
			rapport_Detail_View.lbUpdate1Res.setVisible(true);
            Thread.sleep(1000);
            
            rapport_Detail_View.lbUpdate1Res.setText("[ ok ]");
            rapport_Detail_View.lbUpdate1Res.setForeground(Color.green);	
          } catch (InterruptedException Te) {
        	  System.out.println(Te);
          }			
	}
	
	public void infoUpdate2(){		
		try {
			Thread.sleep(1000);
			progressBar(72);
			rapport_Detail_View.lbUpdate2.setVisible(true);
			rapport_Detail_View.lbEquiv5.setVisible(true);
			rapport_Detail_View.lbUpdate2Res.setVisible(true);
            Thread.sleep(1000);
            
            rapport_Detail_View.lbUpdate2Res.setText("[ ok ]");
            rapport_Detail_View.lbUpdate2Res.setForeground(Color.green);	
          } catch (InterruptedException Te) {
        	  System.out.println(Te);
          }			
	}
	
	public void infoModifProc(){		
		try {
			Thread.sleep(1000);
			progressBar(85);
			rapport_Detail_View.lbProcStock.setVisible(true);
			rapport_Detail_View.lbEquiv6.setVisible(true);
			rapport_Detail_View.lbProcStockRes.setVisible(true);
            Thread.sleep(1000);
            
            rapport_Detail_View.lbProcStockRes.setText("[ ok ]");
            rapport_Detail_View.lbProcStockRes.setForeground(Color.green);	
          } catch (InterruptedException Te) {
        	  System.out.println(Te);
          }			
	}
	
	public void commitOrNot(){		
		try {
			Thread.sleep(1000);
			String msg="Voulez-vous commiter les modifications\n apportées sur le traitement 'PostJNL'?";
			int n = JOptionPane.showConfirmDialog(
					rapport_Panel_View,
				    msg,
				    "Confirmation",
				    JOptionPane.YES_NO_OPTION);
			
			if (n==0){
				System.out.println("BOUTON OK");
				arborCustD3Dao.getUpdate_CPTL_TRT01_POSTJNL_Dao(t_id_run, date1, date2);
				String dateUpdt=date1+"12";
				arborCustD3Dao.getUpdate_JNL_RUNS_STATUS_Dao(dateUpdt, jnlRefNo1, jnlRefNo2);
				arborCustD3Dao.getReplace_Cptl_Trt01_Postjnlv2_Pkg_Dao(t_id_run);
				rapport_Detail_View.rqSuccess.setVisible(true); 
				rapport_Detail_View.rqImportant.setVisible(true); 
			}else{
				System.out.println("BOUTON REFUSE");
			}
		} catch (InterruptedException Te) {
        	  System.out.println(Te);
          }			
	}
	
	public void recapRapport(){		
		try {
			Thread.sleep(1000);
			progressBar(99);
			         
          } catch (InterruptedException Te) {
        	  System.out.println(Te);
          }			
	}
	
	public void progressBar(int value) {
		rapport_Detail_View.barreProgression.setMinimum(0);
		rapport_Detail_View.barreProgression.setMaximum(99);		      
		rapport_Detail_View.barreProgression.setValue(value);
    }
	
	
}


